package com.controller;

import com.alibaba.fastjson.JSONObject;
import com.entity.Seller;
import com.github.pagehelper.Page;
import com.service.SellerService;
import com.util.VeDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController // 定义为控制器 返回JSON类型数据
@RequestMapping(value = "/seller", produces = "application/json; charset=utf-8") // 设置请求路径
@CrossOrigin // 允许跨域访问其资源
public class SellerController { // extends BaseController {
    // TODO Auto-generated method stub

    @Autowired // @Autowired的作用是自动注入依赖的ServiceBean
    private SellerService sellerService;

    // 预处理 获取基础参数
    @GetMapping(value = "createSeller.action")
    public Map<String, Object> createSeller() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("today", VeDate.getStringDateShort());
        return map;
    }

    // 新增商户
    @PostMapping(value = "insertSeller.action")
    public Map<String, Object> insertSeller(@RequestBody String jsonStr) {
        Map<String, Object> map = new HashMap<String, Object>();
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        Seller seller = new Seller();
        seller.setUsername(obj.getString("username")); // 为用户名赋值
        seller.setPassword(obj.getString("password")); // 为密码赋值
        seller.setSellername(obj.getString("sellername")); // 为店铺名称赋值
        seller.setContact(obj.getString("contact")); // 为联系方式赋值
        seller.setAddress(obj.getString("address")); // 为联系地址赋值
        seller.setStatus(""); // 为状态赋值
        seller.setRegdate(VeDate.getStringDateShort()); // 为注册日期赋值
        int num = this.sellerService.insertSeller(seller);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "保存成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "保存失败");
        }
        return map;
    }

    // 按主键删除一个商户
    @GetMapping(value = "deleteSeller.action")
    public Map<String, Object> deleteSeller(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.sellerService.deleteSeller(id);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 按主键批量删除商户
    @PostMapping(value = "deleteSellerByIds.action")
    public Map<String, Object> deleteSellerByIds(@RequestBody String[] ids) {
        int num = 0;
        for (String sellerid : ids) {
            num += this.sellerService.deleteSeller(sellerid);
        }
        Map<String, Object> map = new HashMap<String, Object>();
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "删除成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "删除失败");
        }
        return map;
    }

    // 修改商户
    @PostMapping(value = "updateSeller.action")
    public Map<String, Object> updateSeller(@RequestBody String jsonStr) {
        JSONObject obj = JSONObject.parseObject(jsonStr); // 将JSON字符串转换成object
        Seller seller = this.sellerService.getSellerById(obj.getString("sellerid")); // 获取object中sellerid字段
        seller.setUsername(obj.getString("username")); // 为用户名赋值
        seller.setSellername(obj.getString("sellername")); // 为店铺名称赋值
        seller.setContact(obj.getString("contact")); // 为联系方式赋值
        seller.setAddress(obj.getString("address")); // 为联系地址赋值

        Map<String, Object> map = new HashMap<String, Object>();
        int num = this.sellerService.updateSeller(seller);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "修改失败");
        }
        return map;
    }

    // 更新商户状态
    @GetMapping(value = "status.action")
    public Map<String, Object> status(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        Seller seller = this.sellerService.getSellerById(id);
        String status = "通过审核";
        seller.setStatus(status);
        int num = this.sellerService.updateSeller(seller);
        if (num > 0) {
            map.put("success", true);
            map.put("code", num);
            map.put("message", "修改成功");
        } else {
            map.put("success", false);
            map.put("code", num);
            map.put("message", "修改失败");
        }
        return map;
    }

    // 查询全部商户数据 在下拉菜单中显示
    @GetMapping(value = "getAllSeller.action")
    public List<Seller> getAllSeller() {
        return this.sellerService.getAllSeller();
    }

    // 按关键字查询商户数据 在下拉菜单中显示
    @GetMapping(value = "getSellerMap.action")
    public Map<String, Object> getSellerMap(String keywords) {
        Map<String, Object> map = new HashMap<String, Object>();
        Seller seller = new Seller();
        seller.setUsername(keywords);
        List<Seller> list = this.sellerService.getSellerByLike(seller);
        map.put("data", list);
        return map;
    }

    // 通过AJAX在表格中显示商户数据
    @GetMapping(value = "getSellerByPage.action")
    public Map<String, Object> getSellerByPage(@RequestParam(defaultValue = "1") Integer page,
                                               @RequestParam(defaultValue = "10") Integer limit) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        List<Seller> list = this.sellerService.getAllSeller();
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 通过AJAX在表格中显示商户数据
    @GetMapping(value = "getSeller.action")
    public Map<String, Object> getSeller(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer limit, String keywords) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        Seller seller = new Seller();
        seller.setUsername(keywords);
        List<Seller> list = this.sellerService.getSellerByLike(seller);
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 通过AJAX在表格中显示商户数据
    @GetMapping(value = "getOwnerSeller.action")
    public Map<String, Object> getOwnerSeller(@RequestParam(defaultValue = "1") Integer page,
                                              @RequestParam(defaultValue = "10") Integer limit, String id) {
        // 定义一个Map对象 用来返回数据
        Map<String, Object> map = new HashMap<String, Object>();
        Page<Seller> pager = com.github.pagehelper.PageHelper.startPage(page, limit);// 定义当前页和分页条数
        Seller seller = new Seller();
        // seller.setAdminid(id);
        List<Seller> list = this.sellerService.getSellerByLike(seller);
        // 返回的map中定义数据格式
        map.put("count", pager.getTotal());
        map.put("total", list.size());
        map.put("data", list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("page", page);
        map.put("limit", limit);
        return map;
    }

    // 按主键查询商户数据
    @GetMapping(value = "getSellerById.action")
    public Seller getSellerById(String id) {
        Seller seller = this.sellerService.getSellerById(id);
        return seller;
    }

    // TODO Auto-generated method stub
}
//作者联系 QQ 1420527913   毕设成品下载网址  codebag.cn
